package Study.demo12;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class jdbcDemo2 {
    public static Statement connectData(String Url,String UserName,String Password) {
        Statement statement;
        //加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //与数据库连接
            Connection conn = DriverManager.getConnection(Url, UserName, Password);
            //获取statement对象
            statement= conn.createStatement();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return statement;
    }
    public static List< Worker> selectData1(String sql, Statement statement) throws SQLException {
        ResultSet resultSet=statement.executeQuery(sql);
        List< Worker> workers=new ArrayList<>();
        while(resultSet.next()) {
           String name = resultSet.getString("name");
           int id=resultSet.getInt("id");
           int leader=resultSet.getInt("leader");
           Worker worker = new Worker(name,id,leader);
           workers.add(worker);
        }
        return  workers;
    }
    public static void selectData(String sql,Statement statement,String[] column) throws SQLException {
        ResultSet resultSet=statement.executeQuery(sql);
        while (resultSet.next()) {
            for(int i=0;i<column.length;i++) {
                String str=column[i];
                String res=resultSet.getString(str);
                System.out.print(res+"  ");
            }
            System.out.println();
        }
    }
    public static void updateData(String sql,Statement statement) throws SQLException {
         int isRight=statement.executeUpdate(sql);
        System.out.println(isRight);
    }
    public static void data(List<Worker> list){
        for(int i=0;i<list.size();i++) {
            Worker worker=list.get(i);
            String name=worker.getName();
            int id=worker.getId();
            int leader=worker.getLeader();
            System.out.println(name+" "+id+" "+leader);

        }
    }

    public static void main(String[] args) throws SQLException {
        Statement statement=connectData("jdbc:mysql://localhost:3306/aaa","root","");
        String sql="select * from dept";
        String sql5="select * from worker1";
        List<Worker> wokerlist=selectData1(sql5,statement);
        //data(wokerlist);
        String[] column={"id","name","leader"};
        selectData(sql5,statement,column);
        String[] data={"deptno","dname","loc"};
       // selectData(sql,statement,data);
        String dname="UUUS";
        String loccy="YBKS";
        String sql2="update dept set dname=dname where deptno=10";
        //updateData(sql2,statement);
        String sql1="insert into dept(deptno,dname,loc) values(55,dname,loc)";
        //updateData(sql1,statement);
        String sql3="delete from salgrade where grade=2";
        //updateData(sql3,statement);




    }
}

